package com.yulongtian.offers.day202211.day20221107;

/**
 * https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
 * 可
 * <p>
 * 分组亦或
 *
 * @author yulongTian
 * @create 2022-11-07 10:07
 */
public class Test01 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 10, 4, 1, 4, 3, 3};
        int[] ints = singleNumbers(nums);
        for (int anInt : ints) {
            System.out.print(anInt + ",");
        }
    }

    public static int[] singleNumbers(int[] nums) {
        if (nums.length == 2) {
            return nums;
        }
        int[] res = new int[2];
        int temp = nums[0];
        for (int i = 1; i < nums.length; i++) {
            temp ^= nums[i];
        }

        int index = temp & (-temp);
//        while ((temp & 1) == 0) {
//            index++;
//            temp >>= 1;
//        }

        int res_0 = 0;
        int res_1 = 0;

        for (int i = 0; i < nums.length; i++) {
            //不同
            if ((nums[i] & index) == 0) {
                res_0 ^= nums[i];
            } else {
                res_1 ^= nums[i];
            }
        }
        res[0] = res_0;
        res[1] = res_1;

        return res;
    }
}
